<?php

namespace app\beautiful\model\project;

use app\common\model\CommonModel;



class OnceCardModel extends CommonModel{

    protected  $name = 'beautiful_once_card';

    protected  $pk   = 'once_card_id';



    protected $type=[

        'rotation'=>'array',

    ];



    public function items()

    {

        return $this->hasMany(OnceCardItemModel::class,'once_card_id','once_card_id');

    }





    public function getFlashSaleTimeAttr($value,$data)

    {

        return [$data['start_time'],$data['end_time']];

    }



    public function getSaveMoneyAttr($value,$data){

        $today=date("Y-m-d");

        $noExpire = ($data['start_time']<=$today) && ($data['end_time']>=$today) && ($data['flash_num']>0);

        if($data['is_flash_sale'] == 1 && $noExpire == true){

             return  round(($data['worth'] * 100 - $data['flash_price'] * 100)/100,2);

        }

        return  round(($data['worth'] * 100 - $data['price'] * 100)/100,2);

    }

    

    public function getIsFlashSaleExpireAttr($value,$data)

    {

        $today=date("Y-m-d");

        if($data['start_time']<=$today && $data['end_time']>=$today && $data['flash_num']>0){

            return 0;

        }else{

            return 1;

        }

    }

    



    public function getFlashSaleDjsAttr($value,$data){

        $end_time = strtotime($data['end_time']) + 86400;

        $t = $end_time - time();

        return $t > 0 ? $t : 0;

    }



    public function getValidTimeFormatAttr($value,$data)

    {

        $unit_means=[

            'day'=>'天',

            'month'=>'月',

            'year'=>'年'

        ];

        $valid_unit_means=$unit_means[$data['valid_unit']] ?? '';



        return $data['valid_time'].$valid_unit_means;

    }





}

